//
//  ListenViewController.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-5-13.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ListenViewController.h"
#import "ListenCategoryCell.h"
#import "AppDelegate.h"
#import "ListenSingleCategoryViewController.h"
#import "ListenShelfViewController.h"
#import "SearchViewController.h"

@interface ListenViewController ()
{
    ListenCategoryWidget *listenCategoryWidget;
    ListenSingleCategoryViewController *listenSingleCategoryViewController;
}
@property (nonatomic,retain) NSArray *categoryArray;
@end

@implementation ListenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePlayerBar) name:kPlayerStateDidChange object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.frame = self.parentViewController.view.bounds;
    [self getData];
    [self updatePlayerBar];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (listenCategoryWidget == nil) {
        listenCategoryWidget = [[ListenCategoryWidget alloc] initWithFrame:CGRectMake(0, 0, 320, 548)];
        listenCategoryWidget.delegate = self;
        [listenCategoryWidget updateCategory:_categoryArray];
    }
    if (listenCategoryWidget.superview == nil) {
        [[UIApplication rootViewController] updateWidgetView:listenCategoryWidget];
    }
}

- (void)getData
{
    [self showLoadingView];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSDictionary *data = [[DataRequest sharedRequest] getListenCategory];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self removeloadingView];
            if (data && [data isKindOfClass:[NSDictionary class]] && [[data objectForKey:@"result"] isKindOfClass:[NSArray class]]) {
                self.categoryArray = [data objectForKey:@"result"];
                [categoryTableView reloadData];
                [listenCategoryWidget updateCategory:_categoryArray];
            }
        });
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _categoryArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentify = @"ListenCategoryCell";
    ListenCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[[ListenCategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentify] autorelease];
    }
    [cell setCategory:[_categoryArray objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 155;
}

- (IBAction)showCategory
{
    [[UIApplication rootViewController] showWidgetView];
}

- (void)didSelectCategory:(NSDictionary *)category
{
    if (category) {
        if (listenSingleCategoryViewController == nil) {
            listenSingleCategoryViewController = [[ListenSingleCategoryViewController alloc] initWithNibName:@"ListenSingleCategoryViewController" bundle:nil];
        }
        listenSingleCategoryViewController.category = category;
        listenSingleCategoryViewController.view.frame = self.view.bounds;
        [self.view addSubview:listenSingleCategoryViewController.view];
    }
    else {
        [listenSingleCategoryViewController.view removeFromSuperview];
    }
}

- (IBAction)showShelf:(id)sender {
    ListenShelfViewController *listenShelfViewController = [[[ListenShelfViewController alloc] initWithNibName:@"ListenShelfViewController" bundle:nil] autorelease];
    [[UIApplication rootNavController] pushViewController:listenShelfViewController animated:YES];
}

- (IBAction)searchBtnClicked:(id)sender {
    SearchViewController * searchViewController = [[[SearchViewController alloc]initWithNibName:@"SearchViewController" bundle:nil]autorelease];
    searchViewController.type = searchListen;
    [[UIApplication rootNavController] pushViewController:searchViewController animated:YES];
}

- (void)swipeBack
{
    [self showCategory];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:categoryTableView.frame] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)updatePlayerBar {
    // 判断是否正在播放
    if ([CollectionData hasContent:kCollection_playing]) {
        if (playerBarView) {
            return;
        }
        
        playerBarView = [[[NSBundle mainBundle] loadNibNamed:@"PlayerBarView" owner:self options:nil] objectAtIndex:0];
        CGRect frameTable = categoryTableView.frame;
        CGRect frameBar = playerBarView.frame;
        frameBar.origin.x = frameTable.origin.x;
        frameBar.origin.y = frameTable.origin.y;
        playerBarView.frame = frameBar;
        [self.view addSubview:playerBarView];
        
        frameTable.origin.y += PlayerBarHeight;
        frameTable.size.height -= PlayerBarHeight;
        categoryTableView.frame = frameTable;
    }
    else if (playerBarView) {
        [playerBarView removeFromSuperview];
        playerBarView = nil;
        
        CGRect frameTable = categoryTableView.frame;
        frameTable.origin.y -= PlayerBarHeight;
        frameTable.size.height += PlayerBarHeight;
        categoryTableView.frame = frameTable;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [categoryTableView release];
    [_categoryArray release];
    [listenCategoryWidget release];
    [super dealloc];
}
- (void)viewDidUnload {
    [categoryTableView release];
    categoryTableView = nil;
    [listenCategoryWidget release];
    listenCategoryWidget = nil;
    [listenSingleCategoryViewController release];
    listenSingleCategoryViewController = nil;
    [super viewDidUnload];
}
@end
